//
//  UITableView+EmptyData.m
//  PublicCertification
//
//  Created by 郑程锋 on 2017/7/4.
//  Copyright © 2017年 ZCF. All rights reserved.
//

#import "UITableView+EmptyData.h"

#import <objc/runtime.h>

#define DefaultTipImage @"icon_spray_nodata"

#define TipImageViewWidth 100
#define TipImageViewHeight 100
#define TipLabelHeight 75

@implementation UITableView (EmptyData)

#pragma mark - tipView
- (void)setTipView:(UIView *)tipView {
    objc_setAssociatedObject(self, @selector(tipView), tipView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)tipView {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipImageView
- (void)setTipImageView:(UIImageView *)tipImageView {
    objc_setAssociatedObject(self, @selector(tipImageView), tipImageView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)tipImageView {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipLabel
- (void)setTipLabel:(UILabel *)tipLabel {
    objc_setAssociatedObject(self, @selector(tipLabel), tipLabel,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)tipLabel {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - startTip
- (void)setStartTip:(BOOL)startTip {
    if (startTip) {
        self.tipImage = [UIImage imageNamed:DefaultTipImage];
        if (self.tipView == nil) {
            [self setupViews];
        }
    }
    objc_setAssociatedObject(self, @selector(startTip), @(startTip),
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)startTip {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)ls_layoutSubviews {
    [self ls_layoutSubviews];
    if (self.startTip) {
        //        CGFloat realHeight = self.frame.size.height - self.contentInset.top - self.contentInset.bottom;
        //        if (self.tipView.frame.size.height==0||self.tipView.frame.size.height!=realHeight) {
        //            self.tipView.frame = CGRectMake(0, 0, self.frame.size.width, realHeight);
        //        }
        self.tipView.frame = CGRectMake(0, 0,kScreenWidth , kScreenHeight);
        
        [self settingFrame];
        
    }
}

- (void)settingFrame {
    CGFloat realHeight = self.tipView.frame.size.height;
    CGFloat realWidth = self.tipView.frame.size.width;
    CGSize size = self.bounds.size;
    
    self.tipImageView.frame = CGRectMake((realWidth - TipLabelHeight*2)/2, (realHeight - TipLabelHeight*2)/2 - LL_StatusBarAndNavigationBarHeight, TipLabelHeight*2, TipLabelHeight*2);
    
    
    self.tipLabel.frame = CGRectMake(0, MaxY(self.tipImageView), size.width,20);
    
    
    
}
- (void)setupViews {
    UIView *tipView = [[UIView alloc] init];
    //    tipView.backgroundColor = [UIColor colorWithWhite:0.913 alpha:1.000];
    tipView.backgroundColor = [UIColor whiteColor];
    
    self.tipView = tipView;
    [self addSubview:tipView];
    
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.textColor = [UIColor colorWithHexString:@"#AAAAAA"];
    tipLabel.font = [UIFont systemFontOfSize:13];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [tipView addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    
    UIImageView *tipImageView =
    [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    tipImageView.contentMode = UIViewContentModeScaleAspectFit;
    [tipView addSubview:tipImageView];
    self.tipImageView = tipImageView;
    
//    CGFloat realHeight =self.frame.size.height - self.contentInset.top - self.contentInset.bottom;
    
    //    self.tipView.frame = CGRectMake(0, 0, self.frame.size.width, realHeight);
    self.tipView.frame = CGRectMake(0, 0, self.frame.size.width, kScreenHeight);
    
//    DLog(@"======%lf",realHeight);
//    DLog(@"---------%@",NSStringFromCGRect(self.tipView.frame));
    
    [self settingFrame];
}

#pragma mark - tipImage
- (void)setTipImage:(UIImage *)tipImage {
    self.tipImageView.image = tipImage;
    objc_setAssociatedObject(self, @selector(tipImage), tipImage,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImage *)tipImage {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tipTitle
- (void)setTipTitle:(NSString *)tipTitle {
    
    self.tipLabel.text = tipTitle;
    objc_setAssociatedObject(self, @selector(tipTitle), tipTitle,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)tipTitle {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - tempTitle
- (void)setTempTitle:(NSString *)tempTitle {
    objc_setAssociatedObject(self, @selector(tempTitle), tempTitle,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)tempTitle {
    return objc_getAssociatedObject(self, _cmd);
}


@end

